<?php

class CursoStatusController extends Zend_Controller_Action
{

    protected $_status;

    public function init()
    {
        $this->_status = new CursoStatus();
    }

    public function indexAction()
    {
        $Status = new CursoStatus();
        $this->view->status = $Status->fetchAll(null,'ordem');
    }

    public function addAction()
    {
        $form = new Application_Form_CursoStatus();
        $form->submit->setLabel('Adicionar');
        $form->getElement('ordem')->setValue(1000);
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $status = $form->getValue('status');
                $ordem = $form->getValue('ordem');
                $Status = new CursoStatus();
                $Status->addStatus($status, $ordem);
                $this->organizar();
                $this->_forward('index');
            } else {
                $form->populate($formData);
            }
        }
    }

    public function editAction()
    {
        $form = new Application_Form_CursoStatus();
        $form->submit->setLabel('Salvar');
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $id = (int)$form->getValue('id');
                $status = $form->getValue('status');
                $ordem = $form->getValue('ordem');
                $Status = new CursoStatus();
                $ordem_atual = $Status->find($id)->current()->ordem;
                $Status->updateStatus($id, $status, $ordem);
                if($ordem!=$ordem_atual)
                    $this->organizar();
                $this->_forward('index');
            } else {
                $form->populate($formData);
            }
        } else
        {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $Status = new Status();
                $form->populate($Status->getStatus($id));
            }
        }
    }

    public function deleteAction()
    {
        if ($this->getRequest()->isPost())
        {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Sim') {
                $id = $this->getRequest()->getPost('id');
                $Status = new CursoStatus();
                $Status->deleteStatus($id);
                $this->organizar();
            }
            $this->_forward('index');
        } else {
            $id = $this->_getParam('id', 0);
            $Status = new Status();
            $this->view->status = $Status->getStatus($id);
        }
    }

    private function organizar()
    {
        $Status = new Status();
        $query = $Status->select()->order('ordem');
        $results= $Status->fetchAll($query);
        $cont=0;
        foreach ($results as $value) {
            $cont=$cont+2;
            $value->ordem = $cont;
            $value->save();
        }
    }
}

